#include <iostream>
#include <pthread.h>
#include <stdlib.h>
using namespace std;
class Singleton{
public:
    static Singleton *getInstance(){
        //代表这个init初始化函数只调用一次
      pthread_once(&_once,init);
      return _pInstance;
    }
private:
    static void init()
    {
      _pInstance = new Singleton();
      atexit(destroy);
    }
    static void destroy(){
        if(_pInstance){
            delete  _pInstance;
            _pInstance = nullptr;
        }
    }
    
    Singleton(){
        cout<<"Singleton()"<<endl;
    }
    ~Singleton(){
        cout<<"~Singleton()"<<endl;
    }
    static Singleton * _pInstance;
    static pthread_once_t _once;
};
Singleton * Singleton::_pInstance = nullptr;
pthread_once_t Singleton:: _once = PTHREAD_ONCE_INIT;
void test(){
   Singleton *ps = Singleton::getInstance();
}
int main()
{
    test();
    return 0;
}

